import scipy
import wave
import struct
import numpy
import pylab
import matplotlib.pyplot as plt
from scipy.io import wavfile
audio = wavfile.read('discovery.wav')

print audio[1]

fp = wave.open('disconnect_me.wav', 'rb') # Problem loading certain kinds of wave files in binary?

samplerate = audio[0]
totalsamples = fp.getnframes()
fft_length = 2048 # Guess
num_fft = (totalsamples / fft_length) - 2

temp = numpy.zeros((num_fft, fft_length), float)

leftchannel = numpy.zeros((num_fft, fft_length), float)
rightchannel = numpy.zeros((num_fft, fft_length), float)

for i in range(num_fft):

    tempb = fp.readframes(fft_length / fp.getnchannels() / fp.getsampwidth());

    #tempb = fp.readframes(fft_length)

    up = (struct.unpack("%dB"%(fft_length), tempb))

    #up = (struct.unpack("%dB"%(fft_length * fp.getnchannels() * fp.getsampwidth()), tempb))
    #print (len(up))
    temp[i,:] = numpy.array(up, float) - 128.0


temp = temp * numpy.hamming(fft_length)

temp.shape = (-1, fp.getnchannels())

fftd = numpy.fft.rfft(temp)
print audio[1]
#pylab.plot(audio[1])

plt.plot(audio[1])
plt.ylabel('some numbers')
plt.show()

#pylab.plot(abs(fftd[:1]))

pylab.show()

#Frequency of an FFT should be as follows:

#The first bin in the FFT is DC (0 Hz), the second bin is Fs / N, where Fs is the sample rate and N is the size of the FFT. The next bin is 2 * Fs / N. To express this in general terms, the nth bin is n * Fs / N.
# (It would appear to me that n * Fs / N gives you the hertz, and you can use sqrt(real portion of number*r + imaginary portion*i) to find the magnitude of the signal